package com.ethan.orika;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.*;
import ma.glasnost.orika.CustomConverter;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.MappingContext;
import ma.glasnost.orika.converter.ConverterFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import ma.glasnost.orika.metadata.Type;
import qunar.api.pojo.Money;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ethan
 * create 2018-04-08 下午9:30
 * desc
 **/
public class OrikaTest2 {
    public static void main(String[] args){
        //映射字段名不一致的对象
        Name name = Name.builder().first("li").last("yang").build();
        List<Name> nameList = Lists.newArrayList(new Name("aa","bb"),new Name("cc","dd"));
        List<Good> goodList = Lists.newArrayList(new Good(Money.of(20)),new Good(Money.of(30)));
        Map<String,String> extMap = Maps.newHashMap();
        extMap.put("aaa","111");
        extMap.put("bbb","222");
        extMap.put("ccc","333");
        Person person = Person.builder().id("1").name(name).birthDate(new Date()).knownAliases(nameList).price(null).aaa("2341231235").extMap(extMap).build();


        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();

        ConverterFactory converterFactory = mapperFactory.getConverterFactory();
        converterFactory.registerConverter(new MyConverter());

        mapperFactory.classMap(Person.class,PersonDto.class)
                .field("id","personId")
                .field("name.first", "firstName")
                .field("name.last", "lastName")
                .exclude("sex")
                .field("knownAliases{first}", "aliases{[0]}")
                .field("knownAliases{last}", "aliases{[1]}")
//                .fieldMap("price","price").converter("MyConverter").add()
                .byDefault().register();

//        mapperFactory.getConverterFactory().registerConverter(new CustomConverter<Object, Object>() {
//            @Override
//            public Object convert(Object source, Type<?> destinationType, MappingContext mappingContext) {
//
//
//                return null;
//            }
//        });

        PersonDto personDto =  mapperFactory.getMapperFacade().map(person,PersonDto.class);

        String a="";
    }



    @Setter
    @Getter
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class Person{
        private String id;
        private Name name;
        private int sex;
        private List<Name> knownAliases;
        private Date birthDate;
        private Money price;
//        private List<Good> goodList;
        private String aaa;
        private Map<String,String> extMap;
    }

    @Setter
    @Getter
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class Name{
        private String first;
        private String last;
    }

    @Setter
    @Getter
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class Good{
        private Money allPrice;
    }

    @Setter
    @Getter
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class PersonDto{
        private String personId;
        private String firstName;
        private String lastName;
        private int sex;
        private Date birthDate;
        private String[][] aliases;
        private Money price;
//        private List<Good> goodList;
        private String bbb;
        private Map<String,String> extMap;
    }
}

class MyConverter extends CustomConverter<Money,Money>{
    @Override
    public Money convert(Money source, Type<? extends Money> destinationType, MappingContext mappingContext) {
        if(source == null){
            return Money.of(0);
        }
        return Money.of(source.getAmount());
    }
}


